/**
 * @author David Young
 */
package csab;

import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.RotateTransition;

import csab.core.PlayerStats;
import csab.core.ResourceManager;

/**
 * This state represents the main menu within
 * the game.
 * @author David Young
 */
public class MenuState extends BasicGameState {
	private int stateid;
	private Font fnt;
	private ArrayList<MenuObject> buttonlist;
	private static int BTN_START = 0;
	private static int BTN_OPTIONS = 1;
	private static int BTN_QUIT = 2;
	private int menubtnclicked;
	private int currselected;
	private Image bg;
	
	public MenuState(int id)
	{
		stateid = id;
		menubtnclicked = -1;
		currselected = 0;
	}

	/* (non-Javadoc)
	 * @see org.newdawn.slick.state.GameState#init(org.newdawn.slick.GameContainer, org.newdawn.slick.state.StateBasedGame)
	 */
	@Override
	public void init(GameContainer container, StateBasedGame game) throws SlickException 
	{
		ResourceManager mgr = ResourceManager.getManager("resource/resources.xml");
		
		//Initialize background
		String bgname = "";
		
		float aspect = (float)container.getWidth() / container.getHeight();
		float epsilon = 0.01f;
		
		if(Math.abs(aspect - (float)16/9) < epsilon)
		{
			bgname = "mbg16b9";
		}
		else if(Math.abs(aspect - (float)16/10) < epsilon)
		{
			bgname = "mbg16b10";
		}
		else
		{
			bgname = "mbg4b3";
		}
		
		bg = mgr.getImage(bgname);
		
		//Setup menu font
		fnt = mgr.getFont("speech");
		Color released = Color.yellow;
		Color pressed = Color.white;
		
		//Create menu button list
		buttonlist = new ArrayList<MenuObject>();
		
		//Calculate orientation
		int numitems = 10;
		int offset = container.getHeight() / 2;
		
		//Add buttons to menu
		buttonlist.add(new MenuObject(container.getWidth() / 2, offset + (container.getHeight() - offset) / numitems, 
									  "Start Button", fnt, released, pressed));
		buttonlist.add(new MenuObject(container.getWidth() / 2, offset + (container.getHeight() - offset) / numitems * 2, 
									  "Options", fnt, released, pressed));
		buttonlist.add(new MenuObject(container.getWidth() / 2, offset + (container.getHeight() - offset) / numitems * 3,
									  "Exit Game", fnt, released, pressed));
		
		//Set first button to selected
		buttonlist.get(currselected).handlePressed();
	}

	/* (non-Javadoc)
	 * @see org.newdawn.slick.state.GameState#render(org.newdawn.slick.GameContainer, org.newdawn.slick.state.StateBasedGame, org.newdawn.slick.Graphics)
	 */
	@Override
	public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
		bg.draw(0, 0);
		fnt.drawString((container.getWidth() / 2) - (fnt.getWidth(PlayerStats.getGameName()) / 2), 
				       (container.getHeight() / 10) + (fnt.getHeight(PlayerStats.getGameName()) / 2), 
				       PlayerStats.getGameName(), Color.green);
		for(MenuObject obj : buttonlist)
		{
			obj.draw();
		}
	}

	/* (non-Javadoc)
	 * @see org.newdawn.slick.state.GameState#update(org.newdawn.slick.GameContainer, org.newdawn.slick.state.StateBasedGame, int)
	 */
	@Override
	public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException 
	{
		if(menubtnclicked != -1)
			menuButtonPressed(menubtnclicked, container, game);
	}
	
	/* (non-Javadoc)
	 * @see org.newdawn.slick.state.GameState#keyPressed(int, char)
	 */
	@Override
	public void keyPressed(int key, char c)
	{
		switch(key)
		{
		case Input.KEY_DOWN:
			buttonlist.get(currselected).handleReleased();
			currselected = Math.min(currselected + 1, buttonlist.size() - 1);
			buttonlist.get(currselected).handlePressed();
			break;
		case Input.KEY_UP:
			buttonlist.get(currselected).handleReleased();
			currselected = Math.max(currselected - 1, 0);
			buttonlist.get(currselected).handlePressed();
			break;
		case Input.KEY_ENTER:
			menubtnclicked = currselected;
			break;
		}	
	}
	
	public void menuButtonPressed(int menuid, GameContainer cont, StateBasedGame game) throws SlickException
	{
		menubtnclicked = -1;
		if(menuid == BTN_START)
		{
			game.enterState(EntryPoint.PLAYSTATE_ID, null, new RotateTransition());
		}
		if(menuid == BTN_OPTIONS)
			game.enterState(EntryPoint.OPTIONSSTATE_ID, null, new RotateTransition());
		if(menuid == BTN_QUIT)
			cont.exit();
	}

	/* (non-Javadoc)
	 * @see org.newdawn.slick.state.BasicGameState#getID()
	 */
	@Override
	public int getID() {
		return stateid;
	}
}
